package com.aggron.concurrency.examples.singeton;

import com.aggron.concurrency.annotations.Recommend;
import com.aggron.concurrency.annotations.ThreadSafe;

/**
 * @author LR2SD
 * @description 枚举创建单例--最安全
 * @date 2018-06-10 15:52
 */
@ThreadSafe
@Recommend
public class SingletonExample5 {



    private SingletonExample5(){

    }

    public static SingletonExample5 getInstance(){
        return Singleton.SINGLETON.getSingletonExample5();
    }

    private enum Singleton{
        SINGLETON;
        private SingletonExample5 singletonExample5;

        //jvm会保证线程的安全性，只会调用一次
         Singleton(){
             singletonExample5=new SingletonExample5();
        }

        public SingletonExample5 getSingletonExample5(){
             return singletonExample5;
        }
    }

    public static void main(String [] args){
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }

}
